package com.mycompany.oj.controller;

import com.aliyuncs.exceptions.ClientException;
import com.mycompany.oj.Mapper.Impl.LoginMapperImpl;
import com.mycompany.oj.entity.JsonResult;
import com.mycompany.oj.entity.ResultCode;
import com.mycompany.oj.entity.studentInformation;
import com.mycompany.oj.service.Impl.LoginServiceImpl;
import com.mycompany.oj.util.SharedService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 *
 * @author asus
 */
@RestController
@Api(tags = "LoginController", description = "登录")
public class LoginController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private LoginServiceImpl login;
    @Autowired
    private LoginMapperImpl logDao;

    private SharedService sharedService = new SharedService();

    
    //返回登录情况
    @RequestMapping("/ifload")
    public JsonResult ifload(HttpServletRequest request) throws Exception {
        Object admin = request.getHeader("Authorization");
        if(admin == null)
            return new JsonResult(ResultCode.NOT_LOGIN);
        String username = sharedService.getUsername(request);
        return stringRedisTemplate.hasKey(username) ? new JsonResult(ResultCode.SUCCESS_LOGIN,logDao.getStatus(username).get(0).get("state").toString()):  new JsonResult(ResultCode.NOT_LOGIN) ;
    }

    //退出登录
//    @RequestMapping("/logout")
//    public void logout( HttpServletRequest request) throws Exception {
//        Enumeration em = request.getSession().getAttributeNames();
//        String username = getUsername(request);
//        stringRedisTemplate.delete(username);//根据key删除缓存
//
//        //删除session
//        session.invalidate();
//        while (em.hasMoreElements()) {
//            request.getSession().removeAttribute(em.nextElement().toString());
//        }
//    }

    //登录
    @RequestMapping("/Weblogin")
    public JsonResult doLogin(@RequestParam("username") String username, @RequestParam("password") String password, HttpSession session) {
        stringRedisTemplate.opsForValue().set(username, "true", 60 * 60, TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间 
        List<String> temp = login.loginService(username, password);
        if (temp.get(0).equals("教师") || temp.get(0).equals("学生")) {
            session.setAttribute("username", username);
            return new JsonResult(ResultCode.SUCCESS, temp);
        }
        return new JsonResult(ResultCode.FAIL,temp);
    }

    //忘记密码
    @RequestMapping("/forget_password")
    public JsonResult doForget(@RequestParam("password") String password,@RequestParam("username") String username) {
//        String username = getUsername(session);
        return login.forgetService(username, password);
    }

    //注册账号
    @RequestMapping("/register")
    public JsonResult doRegister(@RequestParam("password") String password, @RequestParam("username") String username, HttpSession session) {

        if (login.registerService(username, password)) {
            session.setAttribute("username", username);
            session.setAttribute("pass", true);
            return new JsonResult(ResultCode.SUCCESS);
        } else {
            return new JsonResult(ResultCode.FAIL,"已经注册过了");
        }
    }

    //注册信息
    @RequestMapping("/register_information")
    public JsonResult doRegisterInformation(HttpServletRequest request) throws Exception {
        String nickname = request.getParameter("nickname");
        String realname = request.getParameter("realname");
        String sex = request.getParameter("sex");
        String school = request.getParameter("school");
        String grade = request.getParameter("grade");
        String major = request.getParameter("major");
        String schoolid = request.getParameter("schoolid");
        String cellphone = sharedService.getUsername(request);

        studentInformation student_information = new studentInformation();
        student_information.setNickname(nickname);
        student_information.setStudent_name(realname);
        student_information.setSex(sex);
        student_information.setSchool(school);
        student_information.setGrade(grade);
        student_information.setMajor(major);
        student_information.setSchoolid(schoolid);

        return new JsonResult(ResultCode.SUCCESS,login.registerInforamtionService(student_information, cellphone));
    }

    //获取头像
    @RequestMapping("/get_picture")
    public JsonResult doGetPicture(HttpServletRequest request) throws Exception {
        String username = sharedService.getUsername(request);
        return login.getPictureService(username);
    }

    //设置头像
    @RequestMapping("/set_picture")
    public JsonResult updatePhotoUrl(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws Exception {
        String username = sharedService.getUsername(request);
        return new JsonResult(ResultCode.SUCCESS,login.setPictureService(username, file));
    }

    //获取短信验证码
    @RequestMapping("/get_message")
    public JsonResult doGetMessage(@RequestParam("phone") String phone) throws ClientException, Exception {
        return login.phoneMa(phone);
    }

    //是否过期
    @RequestMapping("/exceed")
    public JsonResult judgeAdmin(String token) throws Exception {
        return  new JsonResult(ResultCode.SUCCESS,sharedService.judgeAdmin(token));
    }

}
